﻿using de.wolfmarco.pas.Shared.MessageBoxes;
using System;
using System.Reflection;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace de.wolfmarco.pas.Shared.Resources
{
    /// <summary>
    /// Gets a image or ImageSource from the resources by an enum.
    /// </summary>
    public static class Images
    {

        #region MODULE

        public static Image GetModuleImage( string moduleName, Type imageEnumType )
        {
            return loadImage( getModuleImageEnum(moduleName, imageEnumType) ); ;
        }

        public static ImageSource GetModuleImageSource( string moduleName, Type imageEnumType )
        {
            return loadImageSource( getModuleImageEnum( moduleName, imageEnumType ) );
        }

        private static Enum getModuleImageEnum( string moduleName, Type imageEnumType )
        {
            Enum imageEnum = null;
            try
            {
                imageEnum = (Enum)Enum.Parse( imageEnumType, moduleName );
            }
            catch( ArgumentException )
            {
                imageEnum = (Enum)Enum.Parse( imageEnumType, "Placeholder" );
            }
            return imageEnum;
        }

        #endregion MODULE

        #region ImageSource

        public static ImageSource GetImageSource( Image128Enum imageEnum )
        {
            return loadImageSource( imageEnum );
        }

        public static ImageSource GetImageSource( Image64Enum imageEnum )
        {
            return loadImageSource( imageEnum );
        }

        public static ImageSource GetImageSource( Image32Enum imageEnum )
        {
            return loadImageSource( imageEnum );
        }

        public static ImageSource GetImageSource( Image16Enum imageEnum )
        {
            return loadImageSource( imageEnum );
        }

        #endregion ImageSource

        #region Image

        public static Image GetImage( Image128Enum imageEnum )
        {
            return loadImage( imageEnum );
        }

        public static Image GetImage( Image64Enum imageEnum )
        {
            return loadImage( imageEnum );
        }

        public static Image GetImage( Image32Enum imageEnum )
        {
            return loadImage( imageEnum );
        }

        public static Image GetImage( Image16Enum imageEnum )
        {
            return loadImage( imageEnum );
        }

        #endregion Image

        #region PRIVATE

        private static Image loadImage( Enum baseEnum )
        {
            Image image = new Image();
            image.Source = loadImageSource( baseEnum );
            return image;
        }

        private static ImageSource loadImageSource( Enum baseEnum )
        {
            BitmapImage bitmapImage = new BitmapImage();

            bitmapImage.BeginInit();

            StringBuilder uriStringBuilder = new StringBuilder();

            uriStringBuilder.Append( @"pack://application:,,,/" );
            uriStringBuilder.Append( Assembly.GetExecutingAssembly().GetName().Name ); // Gets the assembly name like "de.wolfmarco.pas.Shared"
            uriStringBuilder.Append( ";component/Resources/Images/" );
            uriStringBuilder.Append( baseEnum ); // Gets the enum string like "ModulePlaceholder"

            if( baseEnum is Image128Enum )
            {
                Image128Enum moduleImageEnum = (Image128Enum)baseEnum;
                uriStringBuilder.Append( "_128x128.png" );
            }
            else if( baseEnum is Image32Enum )
            {
                Image32Enum imageEnum = (Image32Enum)baseEnum;
                uriStringBuilder.Append( "_32x32.png" );
            }
            else if( baseEnum is Image16Enum )
            {
                Image16Enum smallImageEnum = (Image16Enum)baseEnum;
                uriStringBuilder.Append( "_16x16.png" );
            }

            // The string is complete like "pack://application:,,,/de.wolfmarco.pas.Shared;component/Resources/Images/ModulePlaceholder.png"
            bitmapImage.UriSource = new Uri( uriStringBuilder.ToString() );

            bitmapImage.EndInit();

            return bitmapImage;
        }

        #endregion PRIVATE
    }

    #region ENUMs

    /// <summary>
    /// Contains the biggest images with the size 128x128.
    /// </summary>
    public enum Image128Enum
    {
        Placeholder,
        StartMenuModule,
        AccountingModule,
        App,
        Exit,
    }

    /// <summary>
    /// Contains big images with the size 64x64.
    /// </summary>
    public enum Image64Enum
    {
        Placeholder,
        StartMenuModule,
        AccountingModule,
        App,
        Exit,
    }

    /// <summary>
    /// Contains normal images with the size 32x32.
    /// </summary>
    public enum Image32Enum
    {
        Placeholder,
        StartMenuModule,
        AccountingModule,
        App,
        Exit,
    }

    /// <summary>
    /// Contains small images with the size 16x16.
    /// </summary>
    public enum Image16Enum
    {
        Placeholder,
        StartMenuModule,
        AccountingModule,
        App,
        Exit,
    } 

    #endregion ENUMs
}
